package com.taotao.service.impl;

import com.taotao.EUTreeNode;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.service.TbItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author evader
 * @date 2020-09-22 21:44
 */
@Service
public class TbItemCatServiceImpl implements TbItemCatService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public List<EUTreeNode> getItemCategoryList(long parentId) {

        // 按照parentId查询分类数据
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(example);

        List<EUTreeNode> treeNodeList = new ArrayList<>();
        // 把查询到的分类数据封装成EasyUI异步树数据结构
        for(TbItemCat itemCat : tbItemCatList) {
            EUTreeNode node = new EUTreeNode();
            node.setId(itemCat.getId());
            node.setText(itemCat.getName());
            node.setState(itemCat.getIsParent() ? "closed" : "open");
            treeNodeList.add(node);
        }
        return treeNodeList;
    }

    @Override
    public TbItemCat selectByPrimaryKey(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }
}

